﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework.Utils
{
    /// <summary>
    /// 时间工具
    /// </summary>
    public class TimeUtils
    {
        /// <summary>
        /// 默认格式: yyyy-MM-dd HH:mm:ss
        /// </summary>
        public const string DefaultFormat = "yyyy-MM-dd HH:mm:ss";

		/// <summary>
		/// 默认日期格式: yyyy-MM-dd
		/// </summary>
		public const string DayFormat = "yyyy-MM-dd";

		/// <summary>
		/// 默认时间格式：HH:mm:ss
		/// </summary>
		public const string TimeFormat = "HH:mm:ss";

        /// <summary>
        /// 毫秒转秒比例(直接把毫秒乘以这个值即可)
        /// </summary>
        public const double SecondRate = 0.001;

        /// <summary>
        /// 秒转毫秒比例(直接把秒乘以这个值即可)
        /// </summary>
        public const long MillisecondRate = 1000L;

        /// <summary>
        /// 1秒时间(s)
        /// </summary>
        public const int SecondTime = 1;
        
		public const long SecondTimeL = SecondTime * MillisecondRate;
		public const int MinuteTime = 60;
		public const long MinuteTimeL = MinuteTime * MillisecondRate;
		public const int HourTime = 60 * MinuteTime;
		public const long HourTimeL = HourTime * MillisecondRate;
		public const int DayTime = HourTime * 24;
		public const long DayTimeL = DayTime * MillisecondRate;
		public const int WeekTime = DayTime * 7;
		public const long WeekTimeL = WeekTime * MillisecondRate;

		/// <summary>
		/// 检测时间间隔
		/// </summary>
		/// <param name="prevTime"></param>
		/// <returns></returns>
		public static long GetIntervalTime(long prevTime)
        {
            long nowTime = DateTime.Now.Ticks;
            return (long)((nowTime - prevTime) * 0.0001);
        }


        /// <summary>
        /// 把 epoch time(新纪元时间) 转换成 DateTime
        /// </summary>
        /// <param name="nowTime"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(long nowTime)
        {
            //计算从1970起始时间
            DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            //计算时间差
            TimeSpan timeSpan = TimeSpan.FromMilliseconds(nowTime);
            dateTime += timeSpan;
            //转换成时区时间
            dateTime = dateTime.ToLocalTime();
            return dateTime;
        }

		/// <summary>
		/// 把 epoch time(新纪元时间) 转换成 DateTime
		/// </summary>
		/// <param name="nowTime"></param>
		/// <returns></returns>
		public static DateTime ToDateTime(int nowTime)
		{
			//计算从1970起始时间
			DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
			//计算时间差
			TimeSpan timeSpan = TimeSpan.FromSeconds(nowTime);
			dateTime += timeSpan;
			//转换成时区时间
			dateTime = dateTime.ToLocalTime();
			return dateTime;
		}
        
		/// <summary>
		/// 把DateTime 转换成新纪元时间
		/// </summary>
		/// <param name="dateTime"></param>
		/// <returns></returns>
		public static long ToEpochTime(DateTime dateTime)
        {
            long currenttimemillis = (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
            return currenttimemillis;
        }

        /// <summary>
        /// 字符串转纪元时间
        /// </summary>
        /// <param name="dateStr"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static long ToEpochTime(string dateStr, string format)
        {
            var finfo = new System.Globalization.DateTimeFormatInfo();
            finfo.ShortDatePattern = format;
            var dateTime = Convert.ToDateTime(dateStr, finfo);
            return ToEpochTime(dateTime);
        }

        /// <summary>
        /// 字符串转纪元时间, 格式: yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="dateStr"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static long ToEpochTime(string dateStr)
        {
            return ToEpochTime(dateStr, DefaultFormat);
        }

        /// <summary>
        /// 毫秒转秒单位
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int ToSecondTime(long time)
        {
            return (int)(time * (float)SecondRate);
        }

        /// <summary>
        /// 获取当前的纪元时间
        /// </summary>
        /// <returns></returns>
        public static long GetCurrentTime()
        {
            return ToEpochTime(DateTime.Now);
        }
        
	}
}
